#!/bin/bash

# DevDeploy CV API 服务启动脚本
# 用法: ./start_cv_api.sh [--classifier <path>] [--segment <path>] [--port <port>] [--host <host>]
# 或者: ./start_cv_api.sh <classifier_path> [segment_path] [port] [host]  (向后兼容)

# 解析参数
MODEL_PATH=""
SEGMENT_MODEL_PATH=""
PORT=8796
HOST="0.0.0.0"

# 检查是否使用新格式（--classifier 或 --segment）
if [[ "$1" == "--classifier" ]] || [[ "$1" == "--segment" ]] || [[ "$1" == "--port" ]] || [[ "$1" == "--host" ]]; then
    # 新格式：使用参数选项
    while [[ $# -gt 0 ]]; do
        case $1 in
            --classifier)
                MODEL_PATH="$2"
                shift 2
                ;;
            --segment)
                SEGMENT_MODEL_PATH="$2"
                shift 2
                ;;
            --port)
                PORT="$2"
                shift 2
                ;;
            --host)
                HOST="$2"
                shift 2
                ;;
            *)
                echo "❌ 未知参数: $1"
                echo "用法: $0 [--classifier <path>] [--segment <path>] [--port <port>] [--host <host>]"
                exit 1
                ;;
        esac
    done
else
    # 旧格式：向后兼容
    if [ $# -lt 1 ]; then
        echo "用法（新格式）: $0 [--classifier <path>] [--segment <path>] [--port <port>] [--host <host>]"
        echo "用法（旧格式）: $0 <classifier_path> [segment_path] [port] [host]"
        echo ""
        echo "参数说明:"
        echo "  --classifier <path>  - 分类模型路径（可选）"
        echo "  --segment <path>     - 分割模型路径（可选）"
        echo "  --port <port>        - 服务端口（可选，默认8796）"
        echo "  --host <host>        - 监听地址（可选，默认0.0.0.0）"
        echo ""
        echo "注意: 至少需要配置一个模型（分类或分割）"
        echo ""
        echo "示例:"
        echo "  # 只配置分类模型"
        echo "  $0 --classifier /path/to/classifier.onnx"
        echo ""
        echo "  # 只配置分割模型"
        echo "  $0 --segment /path/to/segment.onnx"
        echo ""
        echo "  # 同时配置分类和分割模型"
        echo "  $0 --classifier /path/to/classifier.onnx --segment /path/to/segment.onnx"
        echo ""
        echo "  # 向后兼容格式"
        echo "  $0 /path/to/classifier.onnx"
        echo "  $0 /path/to/classifier.onnx /path/to/segment.onnx 8797"
        exit 1
    fi
    
    # 旧格式解析
    MODEL_PATH=$1
    if [ $# -ge 2 ]; then
        SEGMENT_MODEL_PATH=$2
    fi
    if [ $# -ge 3 ]; then
        PORT=$3
    fi
    if [ $# -ge 4 ]; then
        HOST=$4
    fi
fi

# 检查至少配置了一个模型
if [ -z "$MODEL_PATH" ] && [ -z "$SEGMENT_MODEL_PATH" ]; then
    echo "❌ 错误: 至少需要配置一个模型（分类或分割）"
    echo "使用 --classifier 指定分类模型，或使用 --segment 指定分割模型"
    exit 1
fi

# 检查模型文件是否存在
if [ -n "$MODEL_PATH" ] && [ ! -f "$MODEL_PATH" ]; then
    echo "❌ 错误: 分类模型文件不存在: $MODEL_PATH"
    exit 1
fi

if [ -n "$SEGMENT_MODEL_PATH" ] && [ ! -f "$SEGMENT_MODEL_PATH" ]; then
    echo "❌ 错误: 分割模型文件不存在: $SEGMENT_MODEL_PATH"
    exit 1
fi

echo "🚀 启动 DevDeploy CV API 服务"
echo "================================"
if [ -n "$MODEL_PATH" ]; then
    echo "分类模型路径: $MODEL_PATH"
else
    echo "分类模型: 未配置"
fi
if [ -n "$SEGMENT_MODEL_PATH" ]; then
    echo "分割模型路径: $SEGMENT_MODEL_PATH"
else
    echo "分割模型: 未配置"
fi
echo "服务端口: $PORT"
echo "监听地址: $HOST"
echo ""

# 设置环境变量并运行（空值也要设置，让API知道未配置）
if [ -n "$MODEL_PATH" ]; then
    export MODEL_PATH="$MODEL_PATH"
else
    unset MODEL_PATH
fi

if [ -n "$SEGMENT_MODEL_PATH" ]; then
    export SEGMENT_MODEL_PATH="$SEGMENT_MODEL_PATH"
else
    unset SEGMENT_MODEL_PATH
fi

export PORT="$PORT"
export HOST="$HOST"

# 切换到项目根目录（脚本在script目录下，需要回到上一级）
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROJECT_ROOT="$( cd "$SCRIPT_DIR/.." && pwd )"
cd "$PROJECT_ROOT"

# 运行服务
echo "✅ 正在启动服务..."
echo "📖 API文档地址: http://${HOST}:${PORT}/docs"
echo "🏥 健康检查地址: http://${HOST}:${PORT}/health"
echo ""
echo "按 Ctrl+C 停止服务"
echo ""

python api/cv_api.py

